package com.education.demouse.service.impl;

import com.education.common.utils.Convert;
import com.education.demouse.mapper.StudentAnswerDetailsMapper;
import com.education.demouse.pojo.model.StudentAnswerDetails;
import com.education.demouse.pojo.model.UserPaperStatistic;
import com.education.demouse.service.IStudentAnswerDetailsService;
import com.education.welco.pojo.OrderPaper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 老师查看学生答题详情 服务层实现
 *
 * @Author: liftsail
 * @Date: 2022-10-20
 * @Description: 不积跬步无以至千里
 */
@Service
public class StudentAnswerDetailsServiceImpl implements IStudentAnswerDetailsService {

    @Autowired
    private StudentAnswerDetailsMapper studentAnswerDetailsMapper;

    /**
     * 查询老师查看学生答题详情信息
     *
     * @param id 老师查看学生答题详情ID
     * @return 老师查看学生答题详情信息
     */
    @Override
    public StudentAnswerDetails selectStudentAnswerDetailsById(Integer id) {
        return studentAnswerDetailsMapper.selectStudentAnswerDetailsById(id);
    }

    /**
     * 查询老师查看学生答题详情列表
     *
     * @param studentAnswerDetails 老师查看学生答题详情信息
     * @return 老师查看学生答题详情集合
     */
    @Override
    public List<StudentAnswerDetails> selectStudentAnswerDetailsList(StudentAnswerDetails studentAnswerDetails) {
        return studentAnswerDetailsMapper.selectStudentAnswerDetailsList(studentAnswerDetails);
    }

    /**
     * 新增老师查看学生答题详情
     *
     * @param studentAnswerDetails 老师查看学生答题详情信息
     * @return 结果
     */
    @Override
    public int insertStudentAnswerDetails(StudentAnswerDetails studentAnswerDetails) {
        return studentAnswerDetailsMapper.insertStudentAnswerDetails(studentAnswerDetails);
    }

    /**
     * 修改老师查看学生答题详情
     *
     * @param studentAnswerDetails 老师查看学生答题详情信息
     * @return 结果
     */
    @Override
    public int updateStudentAnswerDetails(StudentAnswerDetails studentAnswerDetails) {
        return studentAnswerDetailsMapper.updateStudentAnswerDetails(studentAnswerDetails);
    }

    /**
     * 删除老师查看学生答题详情对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStudentAnswerDetailsByIds(String ids) {
        return studentAnswerDetailsMapper.deleteStudentAnswerDetailsByIds(Convert.toStrArray(ids));
    }

    /**
     * 根据老师id查询学生答题详情列表
     *
     * @param teaId
     * @return
     */
    @Override
    public List<StudentAnswerDetails> queryListByTeaUserId(Integer teaId, Integer classId, Integer paperId) {
        return studentAnswerDetailsMapper.queryListByTeaUserId(teaId, classId, paperId);
    }

    /**
     * 根据老师id查询学生答题统计列表
     *
     * @param teaId
     * @return
     */
    @Override
    public List<StudentAnswerDetails> queryStatisticsListByTeaUserId(Integer teaId, Integer classId, Integer paperId) {
        List<StudentAnswerDetails> studentAnswerDetailsList = studentAnswerDetailsMapper.queryStatisticsListByTeaUserId(teaId, classId, paperId);
        //开启业务处理
        for (StudentAnswerDetails studentAnswerDetails : studentAnswerDetailsList) {
            List<UserPaperStatistic> userPaperStatisticList = new ArrayList<>();
            String paperScoreStr = studentAnswerDetails.getPaperScoreStr();
            String paperNameStr = studentAnswerDetails.getPaperName();
            String[] paperNameSplit = paperNameStr.split(",");
            String[] paperScoreSplit = paperScoreStr.split(",");
            for (int i = 0; i < paperNameSplit.length; i++) {
                UserPaperStatistic userPaperStatistic = new UserPaperStatistic();
                userPaperStatistic.setPaperName(paperNameSplit[i]);
                userPaperStatistic.setPaperScore(Double.valueOf(paperScoreSplit[i]));
                userPaperStatisticList.add(userPaperStatistic);
            }
            studentAnswerDetails.setUserPaperStatisticList(userPaperStatisticList);
        }
        return studentAnswerDetailsList;
    }

    /**
     * 根据id查找试卷详情列表
     *
     * @param teaId
     * @param ids
     * @return
     */
    @Override
    public List<StudentAnswerDetails> queryListByIds(Integer teaId, String ids) {
        //Integer[] integers = Convert.toIntArray(ids);
        return studentAnswerDetailsMapper.queryListByIds(teaId, Convert.toIntArray(ids));
    }

    /**
     * 根据老师id查找试卷列表
     *
     * @param teaId
     * @return
     */
    @Override
    public List<OrderPaper> queryPaperListByTeaId(Integer teaId) {
        return studentAnswerDetailsMapper.queryPaperListByTeaId(teaId);
    }

    @Override
    public int updateStudentAnswerDetailsByPaperId2StuUserId(Integer paperId, Integer userId, BigDecimal score, String comment) {
        return studentAnswerDetailsMapper.updateStudentAnswerDetailsByPaperId2StuUserId(paperId, userId, score, comment);
    }
}